package com.xiaoKe.zhonghzu.global.view;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xiaoKe.zhonghzu.R;
import com.xiaoKe.zhonghzu.databinding.ItemAdapterButtonBinding;
import com.xiaoKe.zhonghzu.global.bean.GeneralData;
import com.xiaoKe.zhonghzu.global.tool.ResourceTool;
import com.xiaoKe.zhonghzu.ui.onClick.OnClickListenerPositionInt;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @ProjectName : yg119
 * @Author : Shijingna
 * @Time : 2024/8/1 08:47
 * @Description : 文件描述
 */
public class ButtonAdapter extends RecyclerView.Adapter<ButtonAdapter.ViewHolder> {
    List<GeneralData> list;

    public ButtonAdapter(List<GeneralData> list) {
        this.list = list;
    }

    int isPosition;

    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        ItemAdapterButtonBinding binding = ItemAdapterButtonBinding.inflate(inflater, parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ViewHolder holder, int position) {
        holder.binding.content.setTextColor(isPosition == position ? ResourceTool.resColor(R.color.colorMain) : ResourceTool.resColor(R.color.color333));
        holder.binding.content.setText(list.get(position).getK().toString());
        if (null!=indexList){
            if (indexList.contains(position)){
                holder.binding.content.setTextColor(ResourceTool.resColor(R.color.color999));
            }
        }
        holder.binding.lineBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    if ( null==indexList || !indexList.contains(position)){
                        onClickListenerPositionInt.onItemClick(position);
                        isPosition = position;
                        notifyDataSetChanged();
                    }
            }
        });

    }

    @Override
    public int getItemCount() {
        return null == list ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        final ItemAdapterButtonBinding binding;

        ViewHolder(ItemAdapterButtonBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    public void setIsPosition(int isPosition) {
        this.isPosition = isPosition;
        notifyDataSetChanged();
    }

    List<Integer> indexList;

    public void setIsPositionNex(List<Integer> indexList) {
        this.indexList = indexList;
        notifyDataSetChanged();
    }

    public OnClickListenerPositionInt onClickListenerPositionInt;

    public void setOnClickListenerPositionInt(OnClickListenerPositionInt onClickListenerPositionInt) {
        this.onClickListenerPositionInt = onClickListenerPositionInt;
    }
}
